import main_activity.csv2excel
import main_activity.excel2csv
import main_activity.format_file
import main_activity.merge
import main_activity.readfile
import main_activity.add_file
import main_activity.change_data

"""
main_activity.excel2csv.xlsx_to_csv('总表.xlsx', 'test.csv')
main_activity.format_file.main("test.csv")
for _ in range(10):
    main_activity.merge.main("test.csv")
main_activity.format_file.main("test.csv")
main_activity.csv2excel.csv_to_xlsx_pd("library\\data.xlsx", "test.csv")
"""


def creat_dataset(name):
    """创建新的数据集"""
    with open("data\\data_list.txt", "a", encoding='utf-8', newline="") as f:
        f.writelines(name + "\n")
        xlsx_path = "data\\" + name + ".xlsx"
        csv_path = "data\\" + name + ".csv"
        main_activity.excel2csv.xlsx_to_csv(xlsx_path, csv_path)
        main_activity.format_file.main(csv_path)
        print("已成功创建新项目" + name)


def print_project_list():
    """打印项目列表"""
    with open("data\\data_list.txt", "r", encoding='utf-8') as f:
        i = 1
        for line in f.readlines():
            line = line.strip("\n")
            print(str(i) + "、" + line)
            i = i + 1


def format_data(name):
    """格式化数据"""
    xlsx_path = "data\\" + name + ".xlsx"
    csv_path = "data\\" + name + ".csv"
    main_activity.excel2csv.xlsx_to_csv(xlsx_path, csv_path)
    main_activity.format_file.main(csv_path)


def print_data_inf(name):
    """打印数据的信息"""
    csv_path = "data\\" + name + ".csv"
    main_activity.readfile.main(csv_path)


def add_data(name, add_file_name):
    """将新文档导入到数据集中"""
    xlsx_path = "data\\" + add_file_name + ".xlsx"
    csv_path = "data\\" + name + ".csv"
    main_activity.add_file.main(xlsx_path, csv_path)
    main_activity.format_file.main(csv_path)
    print("已成功写入数据，请勿重复操作")


def merge_data(name):
    """合并同一人的不同志愿信息"""
    csv_path = "data\\" + name + ".csv"
    d = 0
    for _ in range(10):
        d = d + main_activity.merge.main(csv_path)
    print("已成功合并数据,共计" + str(d) + "人")


def export_data(name):
    """导出数据"""
    print("开始导出数据")
    xlsx_path = "library\\" + name + ".xlsx"
    csv_path = "data\\" + name + ".csv"
    main_activity.csv2excel.csv_to_xlsx_pd(xlsx_path, csv_path)
    print("已成功导出数据")


def search_system(name):
    while main_activity.change_data.main(name) != 0:
        pass


def main():
    """交互程序"""
    print("welcome！")
    print_project_list()
    print("请输入要操作的数据集")
    name = input()
    print("您正在操作的数据集为 " + name)
    while True:
        print("请选择操作")
        print("1、导入新数据（请确保文档在指定位置） 2、查看数据集信息")
        print("3、合并数据                        4、导出为xlsx")
        print("5、数据更改与查询                   6、退出")
        a = input()
        if a == "1":
            print("请输入新文档的名字")
            new_data = input()
            add_data(name, new_data)
        elif a == "2":
            print_data_inf()
        elif a == "3":
            merge_data(name)
        elif a == "4":
            export_data(name)
        elif a == "5":
            search_system(name)
        elif a == "6":
            print("程序已中止")
            exit(0)
        else:
            print("请输入选项前的编号！")


if __name__ == '__main__':
    main()
